<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Db;
class Resume extends Controller
{
	//申请的人员
	public function index()
	{
		$usertel = Session::get('tel');
        $usertel = substr($usertel,0,11);
        $uid = Db::name('user')->field('user_id')->where('user_tel',$usertel)->find();
        $message = Db::name('usermore_information')->where('uid',$uid['user_id'])->find();
        $cname = array();
        $cid = Db::name('company')->field('company_id')->where('company_tel',$usertel)->find();
		$list = Db::name('apply')->where('cid',$cid['company_id'])->where('status',1)->paginate(10);
		foreach($list->items() as $k=>$v){
			$data = Db::name('position')->field('position_name')->where('position_id',$v['pid'])->find();
        	$list->items()[$k]['pid'] = $data['position_name'];
            array_push($cname,$data['position_name']);
        }
		$this->assign('list',$list);
		$this->assign('cname',$cname);
		$this->assign('message',$message);
		return $this->fetch('resume/resume');
	}
	public function getMessage()
	{
		$data = input('post.');
		$message = Db::name('usermore_information')->where('uid',$data['uid'])->find();
		if($message){
			return json($message);
		}

	}
	public function getMessageNum()
	{
		$data = input('post.');

		$viewnum = Db::name('company_recharge')->field('recharge_viewnum')->where('recharge_id',Session::get('companyid'))->find();
		if($viewnum['recharge_viewnum']<=0){
			  return json(['uid'=>0,'msg'=>'查看简历不足,请充值...']);
		}

		$message = Db::name('usermore_information')->where('uid',$data['uid'])->find();
		if($message){
			$num['recharge_viewnum'] = $viewnum['recharge_viewnum'] - 1;
			Db::name('company_recharge')->field('recharge_viewnum')->where('recharge_id',Session::get('companyid'))->update($num);
			return json($message);
		}

	}

	//录取
	public function resume()
	{
		$id = input('get.');
		if(Db::name('apply')->field('id')->where('id',$id['id'])->where('status','3')->find()){
			$arr = ['code'=>0,'msg'=>'已经录取'];
		}else{
			$status['status'] = 3;	
			$pid = Db::name('apply')->field('pid')->where('id',$id['id'])->find();
			$num = Db::name('position')->field('position_num')->where('position_id',$pid['pid'])->find();
			// dump($num);die;
			if($num['position_num']==0){
				$arr = ['code'=>0,'msg'=>'报名人数已满'];
			}else{
				if(Db::name('apply')->where('id',$id['id'])->update($status)){
					$number = $num['position_num']-1;
					$postition['position_num'] = $number;
					if(Db::name('position')->where('position_id',$pid['pid'])->update($postition)){
						$arr = ['code'=>1,'msg'=>'录取成功'];
					}else{
						$arr = ['code'=>0,'msg'=>'录取失败'];
					}
					
				}else{
					$arr = ['code'=>0,'msg'=>'录取失败'];
				}
			}

		}
		return json($arr);
	}

	//拒绝
	public function refuse()
	{
		$id = input('get.');
		$status['status'] = 2;
		if(Db::name('apply')->where('id',$id['id'])->update($status)){
			$arr = ['code'=>1,'msg'=>'录取成功'];
		}else{
			$arr = ['code'=>0,'msg'=>'稍后再试'];
		}
		return json($arr);
	}

	//显示录入人员
	public function luru()
	{
		$usertel = Session::get('tel');
        $usertel = substr($usertel,0,11);
        $uid = Db::name('user')->field('user_id')->where('user_tel',$usertel)->find();
        $message = Db::name('usermore_information')->where('uid',$uid['user_id'])->find();
        $cname = array();
        $cid = Db::name('company')->field('company_id')->where('company_tel',$usertel)->find();
		$list = Db::name('apply')->where('cid',$cid['company_id'])->where('status',3)->paginate(10);
		foreach($list->items() as $k=>$v){
			$data = Db::name('position')->field('position_name')->where('position_id',$v['pid'])->find();
        	$list->items()[$k]['pid'] = $data['position_name'];
            array_push($cname,$data['position_name']);
        }
		$this->assign('list',$list);
		$this->assign('cname',$cname);
		$this->assign('message',$message);
		return $this->fetch('resume/luru');
	}

	//显示拒绝的人员
	public function jujue()
	{
		$usertel = Session::get('tel');
        $usertel = substr($usertel,0,11);
        $uid = Db::name('user')->field('user_id')->where('user_tel',$usertel)->find();
        $message = Db::name('usermore_information')->where('uid',$uid['user_id'])->find();
        $cname = array();
        $cid = Db::name('company')->field('company_id')->where('company_tel',$usertel)->find();
		$list = Db::name('apply')->where('cid',$cid['company_id'])->where('status',2)->paginate(10);
		foreach($list->items() as $k=>$v){
			$data = Db::name('position')->field('position_name')->where('position_id',$v['pid'])->find();
        	$list->items()[$k]['pid'] = $data['position_name'];
            array_push($cname,$data['position_name']);
        }
		$this->assign('list',$list);
		$this->assign('cname',$cname);
		$this->assign('message',$message);
		return $this->fetch('resume/jujue');
	}

	//邀请人才
	public function invitation()
	{

		$data = Db::table('job_user')
				->alias('u')
				->join('job_usermore_information i','u.user_id = i.uid')
				->paginate(10);
		// dump($data->items());die;

		$this->assign('list',$data);
		return $this->fetch('resume/invitation');
	}

	//获取本公司全部职位信息
	public function getPosition()
	{
		$cid = input('get.');
		$data = Db::name('position')->field('position_name,position_id')->where('position_companyid',$cid['cid'])->select();
		if($data){
			return json($data);
		}
	}

	public function yq()
	{
		$data = input('get.');
		$list['pid'] = $data['pid'];
		$list['cid'] = $data['cid'];
		$list['uid'] = $data['uid'];
		$list['addtime'] = time();
		if(Db::name('invitation')->where('pid',$data['pid'])->where('cid',$data['cid'])->where('uid',$data['uid'])->find()){
			$arr = ["code"=>0,"msg"=>"已经邀请过了,不能重新邀请"];
		}else{
			if(Db::name('invitation')->insert($list)){
				$arr = ["code"=>1,"msg"=>"成功"];
			}else{
				$arr = ["code"=>0,"msg"=>"邀请成功"];
			}
		}
		return json($arr);
	}

	public function mytalent()
	{
		$cnames = array();
		$unames = array();
		$pnames = array();
		$array1 = array();
		$array2 = array();
		$newname = array();
		$data = Db::name('invitation')->where('cid',Session::get('companyid'))->select();
		$arrusername = array_column($data,'uid');
		$arrpositionname = array_column($data,'pid');
		$arrcompanyname = array_column($data,'cid');
		$status = array_column($data,'status');
		foreach($arrusername as $k=>$v){
			$usernames = Db::name('usermore_information')->field('username')->where('uid',$v)->find();
			array_push($unames,$usernames['username']);
		}
		foreach($arrcompanyname as $k=>$v){
			$companynames = Db::name('company_information')->field('company_name')->where('company_cid',$v)->find();
			array_push($cnames,$companynames['company_name']);
		}
		foreach($arrpositionname as $k=>$v){
			$positionnames = Db::name('position')->field('position_name')->where('position_id',$v)->find();
			array_push($pnames,$positionnames['position_name']);
		}
		// $this->assign('list',$data);
		$names = array();
		array_push($names,$cnames,$unames,$pnames,$status);
		$len = count($names);
		for($i=0;$i<$len;$i++){
			$arr[] = array_column($names,$i);
		}
		$res = array_filter($arr);
		$this->assign('list',$res);
		return $this->fetch('resume/mytalent');
	}
}